package ipdesk.model.businessInfo.action;

import java.util.List;
import java.util.Map;

import ipdesk.model.businessInfo.bean.BusinessCustomerInfoBean;
import ipdesk.model.businessInfo.bean.BusinessInfoBean;
import ipdesk.model.businessInfo.dao.BusinessInfoDao;
import ipdesk.model.report.promote.bean.PromoteInfoBean;
import ipdesk.model.report.promote.dao.PromoteInfoDao;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;


import ecg.itf.dao.ECGDao;
import ecg.util.PageUtil;

public class BusinessInfoSelectAction  extends ActionSupport implements Preparable, ModelDriven<BusinessInfoBean>
{
	private BusinessInfoBean businessInfoBean;
	
	// 상세보기 뷰페이지에서 사용되는 도메인 객체
	private BusinessInfoBean businessInfoDetail;
	private Map pageInfoDataMap;
	private List<BusinessInfoBean> companyList; 
	private List<BusinessCustomerInfoBean> customerList; 
	private String searchKeyword;
	private String searchWhere;
	private String deskCode;
	private String order;
	private int totalCount;
	
	







	@Override
	public String execute() throws Exception
	{

		totalCount = BusinessInfoDao.getInstance().getDataTotalCount(businessInfoBean);
		
		pageInfoDataMap = PageUtil.getInstance().getPageInfoData(totalCount, businessInfoBean.getPageNumber());
		
		if(businessInfoBean.getDeskCode() != null || !"".equals(businessInfoBean.getDeskCode())){
			//System.out.println("333");
			pageInfoDataMap.put("deskCodeSelected", businessInfoBean.getDeskCode());
			deskCode = businessInfoBean.getDeskCode();
		}
		
		if(businessInfoBean.getOrder() != null || !"".equals(businessInfoBean.getOrder())){
			//System.out.println("333");
			pageInfoDataMap.put("order", businessInfoBean.getOrder());
			order = businessInfoBean.getOrder();
		}
		if(businessInfoBean.getBizOrder() != null || !"".equals(businessInfoBean.getBizOrder())){
			//System.out.println("333");
			pageInfoDataMap.put("bizOrder", businessInfoBean.getBizOrder());
		}
		
		if(businessInfoBean.getSearchKeyword() != null || !"".equals(businessInfoBean.getSearchKeyword())){
			//System.out.println("333");
			pageInfoDataMap.put("searchKeyword", businessInfoBean.getSearchKeyword());
			pageInfoDataMap.put("searchWhere", businessInfoBean.getSearchWhere());
			searchKeyword = businessInfoBean.getSearchKeyword();
			searchWhere	  = businessInfoBean.getSearchWhere();
		}
		
		// 기업정보를 가지고 온다.
		companyList = BusinessInfoDao.getInstance().getPagingDataListForUser(pageInfoDataMap);
		
		
		for(int i=0; i < companyList.size(); i++)
		{
			int companySeq = companyList.get(i).getCompanySeq();
			
			// 해당 기업의 담당자 정보를 가져오는 메서드 호출.
			customerList = BusinessInfoDao.getInstance().getAllCustomerListForCompany(companySeq);
			
			// 여러명의 담당자가 저장된 리스트 객체를 빈객체에 저장.
			companyList.get(i).setBizCustomerInfoBean(customerList);
			
		}
		
		
		
		return SUCCESS;
	}
	
	
	/**
	 * 기업정보 상세보기 데이터 가져오기
	 * @return
	 * @throws Exception
	 */
	public String executeDetail() throws Exception
	{
		/**
		 * 1. 목록에서 넘어온 companySeq를 조건으로 해당 데이터를 DB에서 가지고 온다.
		 *
		 */
		
		int companySeq = businessInfoBean.getCompanySeq();
		
		businessInfoDetail = BusinessInfoDao.getInstance().getDetailData(companySeq);
		
		return SUCCESS;
	}
	
	/**
	 * 기업담당자 정보 상세보기 데이터 가져오기
	 * @return
	 * @throws Exception
	 */
	public String executeCustomerDetail() throws Exception
	{
		/**
		 * 1. 목록에서 넘어온 companySeq를 조건으로 해당 데이터를 DB에서 가지고 온다.
		 * 
		 */
		
		int companySeq = businessInfoBean.getCompanySeq();
		
		// 해당 기업의 담당자 정보를 가져오는 메서드 호출.
		customerList = BusinessInfoDao.getInstance().getAllCustomerListForCompany(companySeq);
		
		return SUCCESS;
	}
	
	/**
	 * 현지업체명 중복확인
	 * @return
	 * @throws Exception
	 */
	public String checkHeadName() throws Exception
	{
		int num = BusinessInfoDao.getInstance().checkHeadName(businessInfoBean.getHeadName());
		
		if(num == 0) {
			businessInfoBean.setCheckFlag(0);
		} else {
			businessInfoBean.setCheckFlag(1);
		}
		
		return SUCCESS;
	}
	
	public Map getPageInfoDataMap()
	{
		return pageInfoDataMap;
	}



	public void setPageInfoDataMap(Map pageInfoDataMap)
	{
		this.pageInfoDataMap = pageInfoDataMap;
	}



	public List getCompanyList()
	{
		return companyList;
	}


	public void setCompanyList(List companyList)
	{
		this.companyList = companyList;
	}
	

	public List<BusinessCustomerInfoBean> getCustomerList()
	{
		return customerList;
	}


	public void setCustomerList(List<BusinessCustomerInfoBean> customerList)
	{
		this.customerList = customerList;
	}


	public BusinessInfoBean getBusinessInfoDetail()
	{
		return businessInfoDetail;
	}


	public void setBusinessInfoDetail(BusinessInfoBean businessInfoDetail)
	{
		this.businessInfoDetail = businessInfoDetail;
	}


	public void prepare() throws Exception
	{
		businessInfoBean = new BusinessInfoBean();
	}
	
	public BusinessInfoBean getModel()
	{
		return businessInfoBean;
	}
	
	public String getSearchKeyword() {
		return searchKeyword;
	}


	public String getSearchWhere() {
		return searchWhere;
	}
	
	public String getDeskCode() {
		return deskCode;
	}
	
	public int getTotalCount() {
		return totalCount;
	}


	public String getOrder() {
		return order;
	}


	public void setOrder(String order) {
		this.order = order;
	}

}
